//---------------------------------------------------------------------------

#include <windows.h>
#include <windowsx.h>
#pragma hdrstop

//---------------------------------------------------------------------------

BOOL registerMainWindow(HINSTANCE hInstance);
LRESULT CALLBACK WindowProcedure(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam);

HWND hMainWindow;
char className[] = "ToXWndClass";



#pragma argsused
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
        MSG msg;
        if (!registerMainWindow(hInstance)) {
                MessageBox(NULL, "Can not register class!", "Error", MB_OK);
                return 0;
        }
        hMainWindow = CreateWindow(className, "ToX Lab1", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, (HWND) NULL, (HMENU) NULL, (HINSTANCE) hInstance, NULL);
        if (!hMainWindow) {
                MessageBox(NULL, "Can not create window!", "Error", MB_OK);
                return 0;
        }
        ShowWindow(hMainWindow, nCmdShow);


        while (GetMessage(&msg, NULL, 0, 0)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
        return msg.wParam;
}


BOOL registerMainWindow(HWND hInstance) {
        WNDCLASSEX windowClass;

        windowClass.cbSize = sizeof(windowClass);
        windowClass.style = CS_HREDRAW | CS_VREDRAW;
        windowClass.lpfnWndProc = WindowProcedure;
        windowClass.cbClsExtra = 0;
        windowClass.cbWndExtra = 0;
        windowClass.hInstance = hInstance;
        windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        windowClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
        windowClass.lpszMenuName = NULL;
        windowClass.lpszClassName = className;
        windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        return RegisterClassEx(&windowClass);
}


LRESULT CALLBACK WindowProcedure(HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        switch (msg) {
                case WM_COMMAND:
                        if (((HWND) lParam == hSomeButton) && (HIWORD(wParam) == BN_CLICKED)) {
                                MessageBox(NULL, "CLICK", "Error", MB_OK);
                        }
      	        break;
                case WM_CLOSE:
      	                DestroyWindow(hWindow);
                break;
                case WM_DESTROY:
      	                PostQuitMessage(0);
                break;
                default:
      	                return DefWindowProc(hWindow, msg, wParam, lParam);
        }
}
